import socket

# 创建一个 TCP 套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定 IP 地址和端口
server_address = ('192.168.3.8', 6688)
server_socket.bind(server_address)

# 开始监听连接
server_socket.listen(1)

# 等待客户端连接
client_socket, client_address = server_socket.accept()
print(f"Connected from {client_address}")

try:
    # 接收客户端发送的数据
    data = client_socket.recv(1024)
    if data:
        print("Received:", data.decode('utf-8'))

        # 向客户端发送响应
        response = "Hello, client!"
        client_socket.send(response.encode('utf-8'))
        print("Sent Hello, client!:", data.decode('utf-8'))
except Exception as e:
    print(f"Error occurred: {e}")
finally:
    # 关闭与客户端的连接
    client_socket.close()
    server_socket.close()
    print("Cloced!")
